import scrapy
from dianyingtiantang.items import DianyingtiantangItem


class DianyingspiderSpider(scrapy.Spider):
    name = "dianyingspider"
    allowed_domains = ["dygod.net"]
    start_urls = ["https://dygod.net/html/gndy/china/index.html"]

    def parse(self, response):

        print(
            "\n\n==============================================================================\n\n"
        )

        a_list = response.xpath('//div[@class="co_content8"]//td[2]//a[2]')

        for a in a_list:
            # 获得第一页name 和要点击的链接
            name = a.xpath("./text()").extract_first()
            href = a.xpath("./@href").extract_first()
            url = "https://dygod.net/" + href

            print(name)

            # 对第二页的链接进行请求访问
            yield scrapy.Request(url=url, callback=self.parse_next, meta={"name": name})

    def parse_next(self, response):
        name = response.meta["name"]
        src = response.xpath('//div[@class="co_content8"]//img/@src').extract_first()
        movie = DianyingtiantangItem(name=name, src=src)
        print(
            "\n\n==============================================================================\n\n"
        )
        yield movie
